var $ = require('./components/zepto/zepto');
require('./components/zepto/event');
require('./components/zepto/ajax');
require('./components/zepto/form');
require('./components/zepto/ie');
require('./components/zepto/touch');

module.exports = $;




 
var swiper=require('./components/swiper/swiper.min.js');
var swiperAnimate = require('./components/swiper/swiper.animate1.0.2.min.js');
var IScroll = require('./components/iscroll/iscroll');


var swiper = new Swiper ('.swiper-container', {
  onInit: function(swiper){ //Swiper2.x的初始化是onFirstInit
    swiperAnimate.swiperAnimateCache(swiper); //隐藏动画元素 
    swiperAnimate.swiperAnimate(swiper); //初始化完成开始动画
  }, 
  onSlideChangeEnd: function(swiper){ 
    swiperAnimate.swiperAnimate(swiper); //每个slide切换结束时也运行当前slide动画
  } 
}) 

function isPassive() {
    var supportsPassiveOption = false;
    try {
        addEventListener("test", null, Object.defineProperty({}, 'passive', {
            get: function () {
                supportsPassiveOption = true;
            }
        }));
    } catch(e) {}
    return supportsPassiveOption;
}
var wx = require('./components/weixin/jweixn');

$("#mainContainer").hide();
$(".swiper-container").hide();
// jquery/zepto
$("#enter").tap(function(){

    myScroll = new IScroll('#wrapper', { mouseWheel: true });
    document.addEventListener('touchmove', function (e) { e.preventDefault(); }, isPassive() ? {
        capture: false,
        passive: false
    } : false);

    $(".swiper-container").hide();
    $("#mainContainer").show();
    
    $.ajax({
        type:'GET',
        url:'http://www.bxing.online/www/php/me.php',
        dataType:'json',
        success:function(data){
            var html = "";
            for(var i=0;i<data.length;i++){
                html +="<li>" + 
                "<h4>"+data[i].name+"</h4>"+
                "<div><img src="+"./images/me/psu.jpg"+"></div>"+
                "<p>"+data[i].sex+"</p>"+
                "<p>"+data[i].age+"</p>"+
                "<p>"+data[i].introduce+"</p>"+
                "</li>";
            }
            $("#scroller ul").html(html);
            setTimeout(function() {
                myScroll.refresh();
            }, 500);
            
        },
        
    })
    
    
    $.ajax({
        type:'POST',
        url:'http://www.bxing.online/wxphp/getsign.php',
        data:{
            url:window.location.href
        },
        dataType:'json',
        success:function(data){
             wx.config({
                debug: false,
                appId: data.appId,
                timestamp:data.timestamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: [
                // 所有要调用的 API 都要加到这个列表中
                'scanQRCode',
                'chooseImage'
                ]
            })

            $('#scan').tap(function(){
                scan();
                // $('.ma').show();
                // startRecognize();
            })
            
             $('#photo').tap(function(){
                photo();
            })
        }
    })
    // $('#close').tap(function(){
    //     scan.close();
    //     $('.ma').hide();
    // })
})


$("#footer li").tap(function(){
    var apiTarget = $(this).attr('id');
    var apiUrl = "http://www.bxing/online/api" + apiTarget + ".php";

    if(apiTarget=='work'){
        $.ajax({
            type:'GET',
            url:'http://www.bxing.online/www/php/work.php',
            dataType:'json',
            success:function(data){
                var html = "";
                for(var i=0;i<data.length;i++){
                html +="<li>" + 
                "<h4>"+data[i].category+"</h4>"+
                "<p>"+data[i].name+"</p>"+
                "<p>"+data[i].time+"</p>"+
                "<p>"+data[i].posts+"</p>"+
                "</li>";
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        })
    }else if(apiTarget=='project'){
        $.ajax({
            type:'GET',
            url:'http://www.bxing.online/www/php/project.php',
            dataType:'json',
            success:function(data){
                var html = "";
                for(var i=0;i<data.length;i++){
                    html +="<li>" + 
                    "<h4>"+data[i].category+"</h4>"+
                    "<p>"+data[i].name+"</p>"+
                    "<p>"+data[i].description+"</p>"+
                    "<p>"+data[i].detail+"</p>"+
                    "<p>"+data[i].tech+"</p>"+
                    "</li>";
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        })
    }else if(apiTarget=='skill'){
        $.ajax({
            type:'GET',
            url:'http://www.bxing.online/www/php/skill.php',
            dataType:'json',
            success:function(data){
                var html = "";
                for(var i=0;i<data.length;i++){
                    html +="<li>" + 
                    "<h4>"+data[i].category+"</h4>"+
                    "<p>"+data[i].name+"</p>"+
                    "<p>"+data[i].time+"</p>"+
                    "<p>"+data[i].level+"</p>"+
                    "</li>";
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        })
    }else if(apiTarget=='me'){
        $.ajax({
            
            type:'GET',
            url:'http://www.bxing.online/www/php/me.php',
            dataType:'json',
            success:function(data){
                
                var html = "";
                for(var i=0;i<data.length;i++){
                    html +="<li>" + 
                    "<h4>"+data[i].name+"</h4>"+
                    "<div><img src="+"./images/me/psu.jpg"+"></div>"+
                    "<p>"+data[i].sex+"</p>"+
                    "<p>"+data[i].age+"</p>"+
                    "<p>"+data[i].introduce+"</p>"+
                    "</li>";
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        })
    }
    $(this).css({"color":"#97d7fa","font-size":"16px"}).siblings('li').css({"color":"#000","font-size":"14px"})
    $(this).children('.iconfont').css({"color":"#97d7fa","font-size":"20px"}).parent('li').siblings('li').children('.iconfont').css({"color":"#000","font-size":"16px"})
})

var interval  = setInterval(function(){
    if(document.readyState === 'complete'){
        clearInterval(interval);
        $("#loading").hide();
        $(".swiper-container").show();
        swiper.updateContainerSize(); // 万分重要
        swiper.updateSlidesSize(); // 万分重要
    }else{
        $("#loading").show();
    }
})

function scan(){
    wx.scanQRCode({
        needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
        scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
        success: function (res) {
        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
      }
    });
  }
function photo(){
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
          var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
      }
    });
  }

// var scan = null;
// function onmarked( type, result ) {
// 	var text = '未知: ';
// 	switch(type){
// 		case plus.barcode.QR:
// 		text = 'QR: ';
// 		break;
// 		case plus.barcode.EAN13:
// 		text = 'EAN13: ';
// 		break;
// 		case plus.barcode.EAN8:
// 		text = 'EAN8: ';
// 		break;
// 	}
// 	alert( text+result );
// }
// function startRecognize() {
// 	scan = new plus.barcode.Barcode('bcid');
// 	scan.onmarked = onmarked; 
// }
// function startScan() {
// 	scan.start();
// }
// function cancelScan() {
// 	scan.cancel();
// }
// function setFlash() {
// 	scan.setFlash();
// }